package com.adcc.ags.cgw.entity.msg;

import com.adcc.ags.cgw.entity.BaseEntity;
import com.adcc.ags.cgw.entity.user.User;
import com.adcc.utility.mq.entity.Message;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.UUID;

/**
 * 核心网关消息实体
 */
public class CGWMsg extends BaseEntity{

    // ID
    private String id;

    // 类型
    private MsgType type = MsgType.DOWNLINK;

    // 发送方
    private String sender;

    // 接收方
    private String receiver;

    // 消息路由地址
    private List<DestRoute> destination = Lists.newArrayListWithExpectedSize(500);

    // 用户配置信息
    private Optional<User> user = Optional.absent();

    // 报文
    private Message msg;

    /**
     * 构造函数
     */
    public CGWMsg(){

    }

    /**
     * 构造函数
     * @param msg
     */
    public CGWMsg(Message msg){
        this.id = UUID.randomUUID().toString();
        this.msg = msg;
    }

    /**
     * 构造函数
     * @param msg
     * @param type
     */
    public CGWMsg(Message msg, MsgType type) {
        this.id = UUID.randomUUID().toString();
        this.msg = msg;
        this.type = type;
    }

    /**
     * 构造函数
     * @param msg
     * @param type
     * @Param sender
     */
    public CGWMsg(Message msg, MsgType type,String sender) {
        this.id = UUID.randomUUID().toString();
        this.msg = msg;
        this.type = type;
        this.sender = sender;
    }

    /**
     * 构造函数
     * @param msg
     * @param type
     * @param destination
     */
    public CGWMsg(Message msg,MsgType type,List<DestRoute> destination){
        this.id = UUID.randomUUID().toString();
        this.msg = msg;
        this.type = type;
        this.destination = destination;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public MsgType getType() {
        return type;
    }

    public void setType(MsgType type) {
        this.type = type;
    }

    public String getSender(){
        return sender;
    }

    public void setSender(String sender){
        this.sender = sender;
    }

    public String getReceiver(){
        return receiver;
    }

    public void setReceiver(String receiver){
        this.receiver = receiver;
    }

    public List<DestRoute> getDestination() {
        return destination;
    }

    public void setDestination(List<DestRoute> destination) {
        this.destination = destination;
    }

    public Optional<User> getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = Optional.fromNullable(user);
    }

    public Message getMsg() {
        return msg;
    }

    public void setMsg(Message msg) {
        this.msg = msg;
    }

    public Optional<String> getChangedReceiver(){
        if(destination == null || destination.size() == 0){
            return Optional.absent();
        }else{
            StringBuilder sb = new StringBuilder();
            for(DestRoute dr : destination){
                sb.append(dr.getUserId()).append(" ");
            }
            if(!Strings.nullToEmpty(receiver).equals(sb.toString().trim())){
                String strReceiver = Strings.nullToEmpty(receiver) + " " + sb.toString().trim();
                return Optional.of(strReceiver.trim());
            }else{
                return Optional.absent();
            }
        }
    }

    @Override
    public Optional<BaseEntity> formatJson(String json) throws Exception {
        return null;
    }

    @Override
    public String toJSON() throws JsonProcessingException {
        return null;
    }
}
